home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / comm / misc / Sashi89.lha / Sashi89 / sources / mui_main.c < prev    next >
C/C++ Source or Header  |  2001-05-08  |  13KB  |  460 lines

  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7.  
  8. #include <proto/intuition.h>
  9. #include <proto/graphics.h>
  10. #include <proto/exec.h>
  11.  
  12. #include <dos/dos.h> /* For FileInfoBlock */
  13.  
  14. #include <libraries/mui.h>
  15. #include <libraries/asl.h>
  16. #include <inline/muimaster.h>
  17.  
  18. //#include "mui.h"
  19.  
  20. #include "protocol.h"
  21.  
  22. #include "mui_tilist.h"
  23.  
  24. #define VERSION " v0.6 ("__DATE__")"
  25.  
  26. struct UtilityBase    *UtilityBase=NULL;
  27. struct IntuitionBase  *IntuitionBase=NULL;
  28. struct GfxBase        *GfxBase=NULL;
  29. struct Library        *MUIMasterBase=NULL;
  30.  
  31.  
  32. Object *App=NULL; /* Application object       */
  33.  
  34. struct MUI_CustomClass *ti_mcc;
  35.  
  36.  
  37. /* Disable auto-library-opening : */
  38. //int __initlibraries=0;
  39.  
  40.  
  41. #define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  42.  
  43.  
  44. #define LISTFILES_SELECT      1
  45. #define LISTFILES_DBLCLICK    2
  46. #define LISTFILES_DELETE      3
  47. #define LISTFILES_PARENT      4
  48. #define LISTFILES_PATHCHANGED 5
  49. #define LISTVARS_SELECT       15
  50. #define LISTVARS_GETVAR       21
  51. #define LISTVARS_SENDVAR      23
  52. #define LISTVARS_GETBACKUP    24
  53.  
  54. BOOL open_libs(void );
  55. void close_libs(void );
  56. void clean_up(int sig);
  57.  
  58. int debug_mode=0; /* used by hard.c */
  59.  
  60. int main(int argc, char **argv)
  61. {
  62.   BOOL running = TRUE;
  63.   ULONG sig;
  64.   Object * main_window; /* Window object            */
  65.   Object * button_listfolders   ; /* Edit Button object       */
  66.   Object * button_update ; /* Delete Button object     */
  67.   Object * button_recv   ;
  68.   Object * button_send   ;
  69.     Object * button_getbackup   ;
  70.   Object * button_parent   ;
  71.   Object * list_files   ;
  72.   Object * list_vars   ;
  73.   Object * text_ti89 ;
  74.   Object * pop_asl;
  75.   Object * string_path;
  76.  
  77.  
  78.   signal(SIGINT,&clean_up);
  79.  
  80.   if ( argc > 1 )
  81.     debug_mode = 1;
  82.  
  83.   if ( ! open_libs() )
  84.   {
  85.     printf("Cannot open libs.\n");
  86.     return(0);
  87.   }
  88.  
  89.   if ( ti_openport() )
  90.     clean_up(0);
  91.  
  92.   //printf("Creating Custom Class\n");
  93.  
  94.   if (!(ti_mcc = MUI_CreateCustomClass(NULL,MUIC_Listview,NULL,sizeof(struct MUITIList_Data),MUITIList_Dispatcher)))
  95.   {
  96.     printf("Cannot create custom class.\n");
  97.     clean_up(0);
  98.   }
  99.  
  100.  
  101.  
  102.   App = ApplicationObject,
  103.         MUIA_Application_Title  , "Sashi89"VERSION,
  104.         MUIA_Application_Version , "$VER: Sashi89 "VERSION,
  105.     MUIA_Application_Copyright , "©1999/2000, Olivier Croquette",
  106.     MUIA_Application_Author  , "Olivier Croquette",
  107.     MUIA_Application_Description, "Link to TI-89.",
  108.     MUIA_Application_Base  , "None",
  109.     SubWindow, main_window = WindowObject,
  110.       MUIA_Window_ID, MAKE_ID('M','A','I','N'),
  111.             MUIA_Window_Title, "Sashi89"VERSION,
  112.       WindowContents, VGroup,
  113.         Child, HGroup,
  114.           Child, VGroup,
  115.             Child, TextObject,
  116.               TextFrame,
  117.               MUIA_Background, MUII_TextBack,
  118.               MUIA_Text_Contents, "Computer",
  119.             End,
  120.             Child, list_files = ListviewObject,
  121.               MUIA_Listview_List, DirlistObject,
  122.                 InputListFrame,
  123.                 MUIA_Dirlist_Directory  , "PROGDIR:",
  124.                 /*MUIA_Dirlist_FilterDrawers, TRUE,*/
  125.                 /*MUIA_Dirlist_AcceptPattern,"#?",*/
  126.                 /*MUIA_List_Format   , "COL=0",*/
  127.                 End,
  128.               End,
  129.               Child,ColGroup(2),
  130.                 Child, KeyLabel2("Path:",'p'),
  131.                 Child, pop_asl = PopaslObject,
  132.                   MUIA_Popstring_String, string_path = KeyString(0,256,'p'),
  133.                   MUIA_Popstring_Button, PopButton(MUII_PopFile),
  134.                           ASLFR_TitleText, "Please select path...",
  135.                           ASLFR_InitialDrawer, "Progdir:",
  136.                           ASLFR_DrawersOnly, TRUE,
  137.                 End,
  138.               End,
  139.             Child, button_send   = SimpleButton("_Send" ),
  140.             Child, button_parent = SimpleButton("_Parent" ),
  141.             End,
  142.           Child, VGroup,
  143.             Child, text_ti89=TextObject,
  144.               TextFrame,
  145.               MUIA_Background, MUII_TextBack,
  146.               MUIA_Text_Contents, "TI89",
  147.             End,
  148.             Child, list_vars = (Object *)NewObject(ti_mcc->mcc_Class, NULL,
  149.               MUIA_Listview_List, ListObject,
  150.                 ReadListFrame,
  151.                 MUIA_Font,                MUIV_Font_Fixed,
  152.                 MUIA_Background,          MUII_ListBack,
  153.                 MUIA_List_AutoVisible,    TRUE,
  154.               End,
  155.               TAG_DONE),
  156.                         Child, button_recv = SimpleButton("_Get var" ),
  157.                         Child, button_getbackup = SimpleButton("_Get backup" ),
  158.             Child, button_listfolders = SimpleButton("_Folders" ),
  159.             Child, button_update = SimpleButton("_Update"),
  160.             End,
  161.           End,
  162.         End,
  163.       End,
  164.     End;
  165.  
  166.   if (!App)
  167.   {
  168.     printf("Failed to create Application ( listvars = %p )\n",list_vars);
  169.     clean_up(0);
  170.   }
  171.  
  172.  
  173.   DoMethod(main_window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,App,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  174.  
  175.   DoMethod(list_files ,MUIM_Notify,MUIA_List_Active,MUIV_EveryTime,App,2,MUIM_Application_ReturnID,LISTFILES_SELECT);
  176.   DoMethod(list_files ,MUIM_Notify,MUIA_Listview_DoubleClick,TRUE,App,2,MUIM_Application_ReturnID,LISTFILES_DBLCLICK);
  177.  
  178.   DoMethod(string_path,MUIM_Notify,MUIA_String_Contents,MUIV_EveryTime,App,2,MUIM_Application_ReturnID,LISTFILES_PATHCHANGED);
  179.  
  180.   DoMethod(button_parent ,MUIM_Notify,MUIA_Pressed,FALSE,App,2,MUIM_Application_ReturnID,LISTFILES_PARENT );
  181.  
  182.     DoMethod(button_getbackup ,MUIM_Notify,MUIA_Pressed,FALSE,App,2,MUIM_Application_ReturnID,LISTVARS_GETBACKUP );
  183.   DoMethod(button_recv ,MUIM_Notify,MUIA_Pressed,FALSE,App,2,MUIM_Application_ReturnID,LISTVARS_GETVAR );
  184.   DoMethod(button_send ,MUIM_Notify,MUIA_Pressed,FALSE,App,2,MUIM_Application_ReturnID,LISTVARS_SENDVAR );
  185.  
  186.   DoMethod(button_update      ,MUIM_Notify,MUIA_Pressed,FALSE,list_vars,1,MUIM_TIList_ListVars);
  187.   DoMethod(button_listfolders ,MUIM_Notify,MUIA_Pressed,FALSE,list_vars,1,MUIM_TIList_ListFolders);
  188.  
  189.   DoMethod(main_window,MUIM_Window_SetCycleChain,list_files,list_vars,button_listfolders,button_update,button_recv,NULL);
  190.  
  191.   set(list_files,MUIA_Dirlist_Directory,(ULONG)"PROGDIR:");
  192.   set(list_vars,MUIA_TIList_ParentApp,(ULONG)App);
  193.   set(list_vars,MUIA_TIList_ParentWindow,(ULONG)main_window);
  194.  
  195.   set(main_window,MUIA_Window_Open,TRUE);
  196.  
  197.   //set(list_vars,MUIA_TIList_CurrentFolder,"toto");
  198.  
  199.   while (running)
  200.   {
  201.     ULONG ret = DoMethod(App,MUIM_Application_Input,&sig);
  202.  
  203.     switch (ret)
  204.     {
  205.       case MUIV_Application_ReturnID_Quit:
  206.       {
  207.         ULONG active;
  208.         get(pop_asl,MUIA_Popasl_Active,&active);
  209.         if (active)
  210.           MUI_Request(App,main_window,0,"Sashi89 error","OK",
  211.                       "Cannot quit now, still some asl popups opened.");
  212.         else
  213.           running = FALSE;
  214.         break;
  215.       }
  216.  
  217.       case LISTFILES_DBLCLICK:
  218.       {
  219.         char newpath[200], *oldpath; /* Todo: 200 */
  220.         struct FileInfoBlock *fib;
  221.  
  222.         DoMethod(list_files,MUIM_List_GetEntry,MUIV_List_GetEntry_Active,&fib);
  223.  
  224.         if ( ! fib || fib->fib_DirEntryType < 0 ) /* if not a valid directory */
  225.           break;
  226.  
  227.         get(list_files,MUIA_Dirlist_Directory,&oldpath);
  228.  
  229.         if ( oldpath[0] != '\0' )
  230.           if ( oldpath[strlen(oldpath)-1] != ':' )
  231.             sprintf(newpath,"%s/%s",oldpath,fib->fib_FileName);
  232.           else
  233.             sprintf(newpath,"%s%s",oldpath,fib->fib_FileName);
  234.         else
  235.           strcpy(newpath,fib->fib_FileName);
  236.  
  237.         set(list_files,MUIA_Dirlist_Directory,(ULONG)newpath);
  238.         break;
  239.       }
  240.  
  241.       case LISTFILES_PATHCHANGED:
  242.       {
  243.         char *content;
  244.         get(string_path,MUIA_String_Contents,&content);
  245.         set(list_files,MUIA_Dirlist_Directory,(ULONG)content);
  246.         break;
  247.       }
  248.  
  249.       case LISTFILES_PARENT:
  250.       {
  251.         int i;
  252.         char newpath[200], *oldpath; /* TODO : 200 */
  253.  
  254.         get(list_files,MUIA_Dirlist_Directory,&oldpath);
  255.  
  256.         for ( i=strlen(oldpath); i>0 && oldpath[i] != ':' && oldpath[i] != '/' ; i--)
  257.           {}
  258.  
  259.         strcpy(newpath,oldpath);
  260.         if ( newpath[i] != ':' )
  261.           newpath[i] = '\0';
  262.         else
  263.           newpath[i+1] = '\0';
  264.  
  265.         set(list_files,MUIA_Dirlist_Directory,(ULONG)newpath);
  266.  
  267.  
  268.         break;
  269.       }
  270.  
  271.             case LISTVARS_GETBACKUP:
  272.       {
  273.                 int code;
  274.  
  275.                 char *filename="backup.89g";
  276.  
  277.                 printf("Getting backup in file %s...",filename);
  278.                 fflush(stdout);
  279.  
  280.                 code = ti_getfolder(NULL,filename);
  281.         if ( code )
  282.           printf("error %d\n",code);
  283.         else
  284.                     printf("OK\n");
  285.         DoMethod(list_files,MUIM_Dirlist_ReRead);
  286.                 break;
  287.             }
  288.  
  289.       case LISTVARS_GETVAR:
  290.       {
  291.         char *whatisclicked;
  292.         char *current_folder="";
  293.  
  294.         get(list_vars,MUIA_TIList_CurrentSelectedVar,&whatisclicked );
  295.  
  296.         if ( whatisclicked[0] == '\0' )
  297.           get(list_vars,MUIA_TIList_CurrentSelectedFolder,&whatisclicked );
  298.         else
  299.           get(list_vars,MUIA_TIList_CurrentFolder, ¤t_folder);
  300.  
  301.         if ( whatisclicked )
  302.         {
  303.           if ( whatisclicked[0] != '.' )
  304.           {
  305.             int code;
  306.             char filename[200],*path; /* TODO : 200 */
  307.  
  308.             get(list_files,MUIA_Dirlist_Directory,&path);
  309.  
  310.             if ( path[strlen(path)-1] == ':' )
  311.               sprintf(filename,"%s%s.89g",path,whatisclicked);
  312.             else
  313.               sprintf(filename,"%s/%s.89g",path,whatisclicked);
  314.  
  315.             printf("Getting %s in file %s...",whatisclicked,filename);
  316.             fflush(stdout);
  317.  
  318.             if ( current_folder[0] ) /* Download var */
  319.               code = ti_getvar(current_folder,whatisclicked,filename);
  320.             else /* Download dir */
  321.               code = ti_getfolder(whatisclicked,filename);
  322.             if ( code )
  323.               printf("error %d\n",code);
  324.             else
  325.               printf("OK\n");
  326.             DoMethod(list_files,MUIM_Dirlist_ReRead);
  327.           }
  328.         }
  329.         else
  330.           printf("Can't get any variable if no TI variable selected !\n");
  331.       }
  332.       break;
  333.  
  334.       case LISTVARS_SENDVAR:
  335.       {
  336.         char *whatisclicked;
  337.  
  338.         get(list_files,MUIA_Dirlist_Path,&whatisclicked);
  339.         if ( whatisclicked )
  340.         {
  341.           int code;
  342.           clock_t clock1=clock(); /*temp. */
  343.           struct FileInfoBlock *fib;
  344.           char *current_folder;
  345.  
  346.           DoMethod(list_files,MUIM_List_GetEntry,MUIV_List_GetEntry_Active,&fib);
  347.           get(list_vars,MUIA_TIList_CurrentFolder, ¤t_folder);
  348.  
  349.  
  350.           printf("Sending %s...",whatisclicked);
  351.           fflush(stdout);
  352.           code = ti_sendfile(whatisclicked,current_folder);
  353.           if ( code )
  354.           {
  355.             printf("error %d\n",code);
  356.             MUI_Request(App,main_window,0,"Sashi error","_OK",
  357.                       ti_code2string(code));
  358.           }
  359.           else
  360.           {
  361.             int time_elaps=(clock()-clock1);
  362.             printf("OK. %ld bytes in %2.2f seconds => %4.0f cps\n",
  363.               (long int)fib->fib_Size,
  364.               ((1.0*time_elaps)/CLK_TCK),
  365.               (CLK_TCK*(1.0*fib->fib_Size)/(time_elaps))
  366.              );
  367.  
  368.             DoMethod(list_vars,MUIM_TIList_Update);
  369.           }
  370.         }
  371.       }
  372.       break;
  373.     }
  374.  
  375.     if (running && sig)
  376.       Wait(sig);
  377.  
  378.     /*if ( ret != 0 )
  379.       printf("fin switch\n");*/
  380.   }
  381.  
  382.   clean_up(0);
  383.  
  384.   return(0);
  385. }
  386.  
  387.  
  388.  
  389. BOOL open_libs(void )
  390. {
  391.  
  392.   if ( !(UtilityBase=(struct UtilityBase *)OpenLibrary((UBYTE *)"utility.library",0)) )
  393.   {
  394.     close_libs();
  395.     return(0);
  396.   }
  397.  
  398.   if ( !(IntuitionBase=(struct IntuitionBase *) OpenLibrary((UBYTE *)"intuition.library",39)) )
  399.   {
  400.     close_libs();
  401.     return(0);
  402.   }
  403.  
  404.   if ( !(GfxBase=(struct GfxBase *) OpenLibrary((UBYTE *)"graphics.library",0)) )
  405.   {
  406.     close_libs();
  407.     return(0);
  408.   }
  409.  
  410.   if ( !(MUIMasterBase=OpenLibrary((UBYTE *)MUIMASTER_NAME,19)) )
  411.   {
  412.     close_libs();
  413.     return(0);
  414.   }
  415.  
  416.   return(1);
  417. }
  418.  
  419.  
  420.  
  421. void close_libs(void )
  422. {
  423.   //printf("closing libs\n");
  424.   if ( UtilityBase )
  425.     CloseLibrary((struct Library *)UtilityBase);
  426.  
  427.   if ( IntuitionBase )
  428.     CloseLibrary((struct Library *)IntuitionBase);
  429.  
  430.   if ( GfxBase )
  431.     CloseLibrary((struct Library *)GfxBase);
  432.  
  433.   if ( MUIMasterBase )
  434.     CloseLibrary(MUIMasterBase);
  435.   //printf("ok\n");
  436. }
  437.  
  438.  
  439. void clean_up(int sig)
  440. {
  441.   sig = sig; /* avoid warning because sig is not used */
  442.  
  443.   //printf("cleanup\n");
  444.  
  445.   close_libs();
  446.  
  447.   //printf("dispose app\n");
  448.   if (App)
  449.     MUI_DisposeObject(App);
  450.   //printf("dispose cc\n");
  451.   if ( ti_mcc )
  452.     MUI_DeleteCustomClass(ti_mcc);
  453.  
  454.   //printf("close port\n");
  455.   ti_closeport();
  456.   //printf("exit\n");
  457.   exit(0);
  458. }
  459.  
  460.